package com.alex.service.impl;

import com.alex.entity.User;
import com.alex.exception.UserNameNotFoundException;
import com.alex.service.UserDetailService;
import com.alex.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserDetailServiceImpl implements UserDetailService {

    @Resource
    private UserService userService;

    @Override
    @Transactional
    public void updateLoginCount(User user) throws UserNameNotFoundException{
        user.setLastLoginErrorTime(new Date(System.currentTimeMillis()));
        if (user.getLoginErrorCount() == null) {
            user.setLoginErrorCount(0);
        }
        user.setLoginErrorCount(user.getLoginErrorCount() + 1);
        userService.loginErrorCount(user);
        throw new UserNameNotFoundException("请检查用户名和密码");
    }
}
